package com.iSnail.ms.gather.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.iSnail.ms.util.Config;
import com.iSnail.ms.util.I18N;

public class AboutDialog extends JDialog {
	private JPanel panel1;
	private JLabel imagejLabel;
	private JLabel labelSystemName;
	private JLabel labelCorporation;
	private JLabel labelCopyright;
	private JLabel labelVersion;
	private JLabel labelWebSite;
	private Icon iconAbout;
	private String softwareProductName;
	private String version;
	private String copyright;
	private GridBagLayout gridBagLayout2;
	private JFrame parent = null;

	public AboutDialog(JFrame frame) {
		super(frame, "", true);
		setLocationRelativeTo(frame);
		this.parent = frame;
		this.panel1 = new JPanel();
		this.labelSystemName = new JLabel();
		this.labelCorporation = new JLabel();
		this.labelCopyright = new JLabel();
		this.labelVersion = new JLabel();
		this.labelWebSite = new JLabel(Config.getConfigValue("company.name"));
		this.softwareProductName = "";
		this.version = "V1.0";
		this.copyright = "";
		this.gridBagLayout2 = new GridBagLayout();
		try {
			jbInit();
			pack();
		} catch (Exception ignore) {
			ignore.printStackTrace();
		}
	}

	private void jbInit() throws Exception {
		this.panel1.setLayout(this.gridBagLayout2);
		this.iconAbout = new ImageIcon("resource/pic/about.jpg");
		this.imagejLabel = new JLabel(this.iconAbout, 0);
		if (this.softwareProductName.equals("")) {
			this.softwareProductName = I18N.getString("app_name");
		}
		if (this.copyright.equals("")) {
			this.copyright = "copyright 2008 - " + Calendar.getInstance().get(Calendar.YEAR);
		}
		this.labelSystemName.setText(this.softwareProductName + "(" + this.version + ")");
		this.labelCorporation.setText(Config.getConfigValue("author") + " " + this.copyright);
		this.labelCopyright.setText(this.copyright);
		this.labelVersion.setText(this.version);
		setTitle(this.softwareProductName);

		this.panel1.add(this.imagejLabel, new GridBagConstraints(0, 0, 1, 1, 0.0D, 0.0D, 17, 2, new Insets(12, 12, 5, 12), 1, 1));
		this.panel1.add(this.labelSystemName, new GridBagConstraints(0, 1, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(5, 12, 2, 20), 0, 0));
		this.panel1.add(this.labelCorporation, new GridBagConstraints(0, 3, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(5, 12, 2, 20), 0, 0));
		this.panel1.add(this.labelWebSite, new GridBagConstraints(0, 2, 1, 1, 0.0D, 0.0D, 17, 0, new Insets(5, 12, 2, 20), 0, 0));

		
		getContentPane().add(this.panel1);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
		setResizable(false);
		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				this_keyPressed(e);
			}
		});
	}

	void this_keyPressed(KeyEvent e) {
		if ((e.getKeyCode() == 27) || (e.getKeyCode() == 10))
			dispose();
	}

	public void show() {
		try {
			adjustLocationToParentWindowCenter();
			super.show();
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					requestFocus();
				}
			});
		} catch (Exception ignore) {
			ignore.printStackTrace();
		}
	}

	private void adjustLocationToParentWindowCenter() {
		if (this.parent != null) {
			int w = getWidth();
			int h = getHeight();
			Dimension screen = this.parent.getSize();
			if (w > screen.width) {
				w = screen.width;
			}
			if (h > screen.height) {
				h = screen.height;
			}
			int x = this.parent.getX() + (screen.width - w) / 2;
			int y = this.parent.getY() + (screen.height - h) / 2;
			setLocation(x, y);
		}
	}
	public static void main(String[] args) {
		new AboutDialog(new JFrame()).show();
	}
}